package com.neu.questionnaireplus.service.impl;

import com.neu.questionnaireplus.dao.AdminDao;
import com.neu.questionnaireplus.entity.Admin;
import com.neu.questionnaireplus.service.AdminService;
import com.neu.questionnaireplus.utils.common.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AdminServiceImpl implements AdminService {

  @Autowired
  private AdminDao adminDao;

  @Override
  public boolean loginByPassword(String username, String password) {
    Admin t = null;
    t = adminDao.queryAdminByUsername(username);
    if (t == null){
      throw new RuntimeException("管理员不存在");
    }else {
      if (t.getPassword().equals(password)){
        t.setLastLoginDate(CommonUtils.getDateByDateString(CommonUtils.getFormatDateTimeNow()));
        boolean res = updateAdmin(t);
        return res;
      }else {
        throw new RuntimeException("密码错误");
      }
    }
  }

  @Override
  public boolean loginByPhone(String phone, String inputStr, String realStr) {
    Admin admin = adminDao.queryAdminByPhone(phone);
    if (admin != null){
      if (inputStr.equals(realStr)){
        return true;
      }else {
        throw new RuntimeException("手机验证码不正确");
      }
    }else {
      throw new RuntimeException("该手机号下的管理员不存在！");
    }
  }

  @Override
  public boolean checkStr(String phone, String inputStr, String realStr) {
    Admin admin = adminDao.queryAdminByPhone(phone);
    if (admin != null){
//      System.out.println(inputStr + " " + realStr);
      if (inputStr.equals(realStr)){
        return true;
      }else {
        throw new RuntimeException("手机验证码不正确");
      }
    }else {
      throw new RuntimeException("不存在该管理员");
    }
  }

  @Transactional
  @Override
  public boolean updateAdmin(Admin admin) {
    if (admin != null && !admin.getId().equals("")){
      try {
        String username = admin.getUsername();
        String telnumber = admin.getTelnumber();
        Admin admin1 = adminDao.queryAdminByPhone(telnumber);
        Admin admin2 = adminDao.queryAdminByUsername(username);
        if ((admin1 != null || admin2 != null) && ( (admin1 != null && !admin.getId().equals(admin1.getId())) || (admin2 != null && !admin.getId().equals(admin2.getId()))) ){
          throw new RuntimeException("修改的用户名或手机号与其他用户有冲突");
        }else {
          int i = adminDao.updateAdmin(admin);
          if (i == 1) return true;
          else throw new RuntimeException("更新管理员失败！！" + admin);
        }
      }catch (Exception e){
        throw new RuntimeException("更新管理员失败！！" + e.getMessage());
      }
    }else {
      throw new RuntimeException("更新管理员失败,管理员id不能为空");
    }
  }

  @Override
  public Admin queryAdminByUsername(String username) {
    return adminDao.queryAdminByUsername(username);
  }

  @Override
  public Admin queryAdminByPhone(String phone) {
    return adminDao.queryAdminByPhone(phone);
  }
}
